Lab 21

Cell Arrays

CS211 Lab Policy:

Instructions:

You will create one MATLAB program named Lab21 in file Lab21.m for this lab.  The instructions below ask you to perform some of the same plotting your did in Lab 17 (i.e., plotting data from the 2007 Pikes Peak Marathon) -- but this time the data will be in cell arrays.

  1. Save the file PikesPeakMarathon2007.txt to your current MATLAB working directory. (This is the same data file we used for Lab 17. If you still have this data file from Lab 17, you can skip this step.)
     
  2. Clear the command window.
     
  3. Use the series of three MATLAB statements below to read the race data from a file into a cell array. The cell array name is Data.

    fid = fopen('PikesPeakMarathon2007.txt', 'r');
    Data = textscan(fid, '%d %d%*c%d %24c %d %c %15c %2c %7c %7c %7c');
    fclose(fid);


    Do not attempt to understand this textscan() command at this time. We will study how to read data from a file later in the semester.

    The cell array, Data, contains the following values:
     
    Cell Array Description Size (Type)
    Data{1} The runner's finishing place (e.g., 1st, 2nd, 3rd, etc.) 882x1 (int32)
    Data{2} The runner's finishing place in their age category 882x1 (int32)
    Data{3} The total number of runners in this runner's age category 882x1 (int32)
    Data{4} The runner's name 882x24 (char)
    Data{5} The runner's age 882x1 (int32)
    Data{6} The runner's gender 882x1 (char)
    Data{7} The runner's home town 882x15 (char)
    Data{8} The runner's home state (or country) 882x2 (char)
    Data{9} The time it took for the runner to get to the top of Pikes Peak 882x7 (char)
    Data{10} The time it took for the runner to get down from the top of Pikes Peak 882x7 (char)
    Data{11} The total race time for the runner 882x7 (char)

     

  4. Use the disp(Data) command to display the structure of the cell array. Notice that you have three types of data in the cell array -- column vectors of int32 (32 bit integer) values, a column vector of characters (for gender), and six 2D arrays of characters.
     
  5. The runners' names and home towns are stored in 2D arrays of characters, which is not ideal for processing because each string is padded with blanks to make all the strings the same length. (All 2D arrays in MATLAB are rectangular -- all rows must have the same number of columns.) Convert the runners' names and home towns (Data{4} and Data{7}) from 2D char arrays to cell arrays using the cellstr() function. For example:

        Data{4} = cellstr( Data{4} );
     
  6. Use the disp(Data) command again to display the structure of the cell array. Notice the entries for the 4th and 7th elements. They are now cell arrays. You now have a nested data structure -- some of the cells in Data point to other cell arrays!
     
  7. Before we begin plotting, let's make sure we can get specific data values out of the cell array. Using fprintf(), display the following values:
  8. Now its time to plot! Open your MATLAB solution to Lab 17 and copy/paste your code Lab 17 code into your Lab21 file. Then modify it to plot the same graphs you produced for Lab 17. The specific plots are listed below. It is suggested that you comment out most of the code you just copied and get one plot to work correctly at a time.
     
    1. Add code to count the number of males and females in the race.
       
    2. Create a pie chart that shows the percentage of male and female runners. Include an appropriate graph title and legend.
       
    3. Add the following lines to your code. In addition, add these lines after every graph produced below so that you can see each individual graph.

          % wait for user to press enter, then close the figure window

          Wait = input('Press enter to continue.', 's');
          close()

       
    4. Add code to count the number of runners in each of the age groups listed below. (If you use array comparisons, they must be combined with the single logical operators (& and |), not (&& and ||)). Include an appropriate graph title and legend.
    • under 20
    • 20-29
    • 30-39
    • 40-49
    • 50-59
    • 60-69
    • 70 and over
    1. Add code to show the distribution of runners based on their ages. Use a bin centered at the middle of each age category (i.e., 15, 25, 35, etc.). Include an appropriate graph title and axis labels.
       
    2. Add code to show a comparison of the number of runners from Arkansas (AR), Colorado (CO), Texas (TX), and New Mexico (NM). Use the strmatch('target', string_vector) function to find matching states. Include an appropriate graph title and bar labels.
       
    3. Add code to show a comparison between the age of runners and their finishing place in the race. Include an appropriate graph title and axis labels.
       
    4. You can stop here, or you can generate a subplot chart. If you continue, create a single figure window that includes 4 separate plots using the subplot() function. You can copy/paste the code from previous steps to create this figure.

Turn-in:

Submit your Lab21.m file.